﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace FilterSort
{
	public class ArticleKeywordBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getArticleKeywordSetKeywords(IApplication app, IRuleContext context, Swaf.BGuid.Guid setId)
		{
			FlexiMap m = new FlexiMap();
			m.put("setId", setId);
			IBizObjList keywords = app.appData.get("DA.Articles.GetArtcleKeywordSetKeywords", m) as IBizObjList;
			m.put("_list", keywords);
			app.appData.get("DA.Articles.GetArticleKeywordMatchCounts", m);
			return keywords;
		}

		public void updateArticleSetKeyword(IApplication app, IRuleContext context, IBizObj keyword, Swaf.BGuid.Guid articleId)
		{
			FlexiMap m = new FlexiMap();
			m.put("companyId", ActiveCompanyId);
			keyword.put("companyId", ActiveCompanyId);
			if (keyword.isNew)
			{
				app.appData.put("DA.Articles.AddNewArticleKeywordSetKeyword", keyword);
			}
			else
			{
				app.appData.put("DA.Articles.UpdateArticleKeywordSetKeyword", keyword);

				m.put("keywordId", keyword.get("id"));
				app.appData.put("DA.Articles.DeleteAllArticleKeywordMatches", m);
			}


			FlexiMap info = new FlexiMap();
			info.put("parentId", articleId);
			info.put("listDetailLevel", DetailLevel.idOnly);
			info.put("includeSelf", true);
			info.put("entireTree", true);

			IResults rc = context.RuleGroup.execute(context, "getArticles", info, 
				getFilterSortCriteriaFromKeyword(app, context, (string)keyword.get("keyword"),false));
			IBizObjList articles = rc.CallResults as IBizObjList;
			if (articles != null)
				foreach (IBizObj article in articles)
				{
					m.put("id", new Swaf.BGuid.Guid());
					m.put("articleId", article.get("id"));
					app.appData.put("DA.Articles.AddNewArticleKeyworkdMatch", m);
				}
		}

		public IBizObj getFilterSortCriteriaFromKeyword(IApplication app, IRuleContext context, string keyword, bool onlyCatagories)
		{
			IBizObj filterOp = app.bizObjMgr.create("FilterSortOptions");
			filterOp.put("filterIsAnd", false);
			IBizObjList filters = filterOp.get("filterOptions") as IBizObjList;
			string[] keywords = keyword.Split(',');
			foreach (string kw in keywords)
			{
				if (!Cmn.IsEmpty(kw))
				{
					IBizObj filt = filters.insertNew();
					filt.put("field", "baseTitle");
					filt.put("criteria", "c");
					filt.put("value", kw);

					filt = filters.insertNew();
					filt.put("field", "summary");
					filt.put("criteria", "c");
					filt.put("value", kw);

					filt = filters.insertNew();
					filt.put("field", "body");
					filt.put("criteria", "c");
					filt.put("value", kw);
				}
			}
			if (onlyCatagories)
			{
				IBizObj filt = filters.insertNew();
				filt.put("field", "a.metaType");
				filt.put("criteria", "=");
				//TODO: Clean this up.  Articles and products dont use the same name for a "catagory".  They should.
				filt.put("value", "-Department");
			}
			return filterOp;
		}

		public void deleteArticleSetKeyword(IApplication app, IRuleContext context, Swaf.BGuid.Guid keywordId)
		{
			FlexiMap m = new FlexiMap();
			m.put("id", keywordId);
			app.appData.put("DA.Articles.DeleteArticleKeywordSetKeyword", m);
		}

		public void updateArticleKeywordSet(IApplication app, IRuleContext context, IBizObj keywordSet)
		{
			keywordSet.put("companyId", ActiveCompanyId);
			if (keywordSet.isNew)
				app.appData.put("DA.Articles.AddNewArticleKeywordSet", keywordSet);
			else
				app.appData.put("DA.Articles.UpdateArticleKeywordSet", keywordSet);
		}

		public void deleteArticleKeywordSet(IApplication app, IRuleContext context, Swaf.BGuid.Guid setId)
		{
			FlexiMap m = new FlexiMap();
			m.put("id", setId);
			app.appData.put("DA.Articles.DeleteArticleKeywordSet", m);
		}

		public void updateArticleKeywordMatchSet(IApplication app, IRuleContext context, IBizObj matchSet)
		{
			if (matchSet.isNew)
				app.appData.put("DA.Articles.AddNewArticleKeywordMatchSet", matchSet);
			else
				app.appData.put("DA.Articles.UpdateArticleKeywordMatchSet", matchSet);
		}

		public IBizObj createNewArticleMatchSetIfNotExist(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId, string setName)
		{
			FlexiMap m = new FlexiMap();
			m.put("companyId", ActiveCompanyId);
			m.put("name", setName);
			m.put("articleId", articleId);
			IBizObj articleSet = app.appData.get("DA.Articles.GetArticleSetFromName", m) as IBizObj;
			if (articleSet == null)
			{
				articleSet = app.bizObjMgr.create("ArticleKeywordSet");
				articleSet.put("id", new Swaf.BGuid.Guid());
				articleSet.put("name", setName);
				articleSet.put("companyId", ActiveCompanyId);
				app.appData.put("DA.Articles.AddNewArticleKeywordSet", articleSet);
				m.put("id", new Swaf.BGuid.Guid());
				m.put("setId", articleSet.get("id"));
				app.appData.put("DA.Articles.AddNewArticleKeywordMatchSet", m);
			}


			return articleSet;
		}
	}
}
